package com.siwei.mes.service.experiment.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.siwei.mes.common.query.PageQuery;
import com.siwei.mes.entity.experiment.ExperimentPrintRecord;
import com.siwei.mes.mapper.experiment.ExperimentPrintRecordMapper;
import com.siwei.mes.service.experiment.ExperimentPrintRecordService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

import static com.siwei.mes.common.exception.handler.MessageHandler.assertTrue;

/**
 * 打印记录(ExperimentPrintRecord)表服务实现类
 *
 * @author linzi
 * @since 2024-07-27 18:22:12
 */
@Service
public class ExperimentPrintRecordServiceImpl implements ExperimentPrintRecordService {
    @Resource
    private ExperimentPrintRecordMapper experimentPrintRecordMapper;

    @Override
    public void create(ExperimentPrintRecord entity) {
        List<Long> experimentIdList = entity.getExperimentIdList();

        for (Long experimentId : experimentIdList) {
            entity.init(true);
            entity.setId(null);
            entity.setExperimentId(experimentId);
            experimentPrintRecordMapper.insertSelective(entity);
        }
    }

    @Override
    public void update(ExperimentPrintRecord entity) {
        ExperimentPrintRecord experimentPrintRecord = experimentPrintRecordMapper.selectByPrimaryKey(entity.getId());
        assertTrue(experimentPrintRecord == null, "打印记录信息不存在");
        entity.init(true);
        experimentPrintRecordMapper.updateByPrimaryKeySelective(entity);
    }

    @Override
    public void delete(Long id) {
        ExperimentPrintRecord experimentPrintRecord = experimentPrintRecordMapper.selectByPrimaryKey(id);
        assertTrue(experimentPrintRecord == null, "打印记录信息不存在");
        experimentPrintRecordMapper.deleteByPrimaryKey(id);
    }

    @Override
    public PageInfo<ExperimentPrintRecord> getPage(PageQuery<ExperimentPrintRecord, ExperimentPrintRecord> query) {
        ExperimentPrintRecord searchParam = query.getParams();
        return PageHelper.startPage(query.getPageNum(), query.getPageSize())
                .doSelectPageInfo(() -> experimentPrintRecordMapper.getList(searchParam));
    }

    @Override
    public ExperimentPrintRecord loadById(Long id) {
        return experimentPrintRecordMapper.loadById(id);
    }

    @Override
    public List<ExperimentPrintRecord> getAll(ExperimentPrintRecord entity) {
        return experimentPrintRecordMapper.getList(entity);
    }
}
